//
//  YLContinuationOrderController.m
//  YLFinanceService
//
//  Created by she1323 on 2017/9/14.
//  Copyright © 2017年 she1323. All rights reserved.
//  可续投订单

#import "YLContinuationOrderController.h"
#import "YLRegularFinancingTableViewCell.h"
#import "YLRegularFinancingDetailController.h"
#import "YLContinuationOrderViewModel.h"
#import "YLContinuationOrderModel.h"
@interface YLContinuationOrderController ()
@property (nonatomic, strong) YLContinuationOrderViewModel *viewModel;
@end

@implementation YLContinuationOrderController

- (YLContinuationOrderViewModel *)viewModel
{
    if (!_viewModel) {
        _viewModel = [[YLContinuationOrderViewModel alloc] init];
    }
    return _viewModel;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"可续投订单";
    
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([YLRegularFinancingTableViewCell class]) bundle:nil] forCellReuseIdentifier:[YLRegularFinancingTableViewCell cellID]];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"sort"] style:UIBarButtonItemStylePlain target:self action:@selector(sort)];

    __weak typeof(self) weakSelf = self;
    [[self.viewModel.requestCommand execute:nil] subscribeNext:^(id  _Nullable x) {
        [weakSelf setEmpty];
        [weakSelf.tableView reloadData];
    }];

}

- (instancetype)init
{
    return [self initWithStyle:UITableViewStyleGrouped];
}

- (void)sort
{
    //2.倒序的数组
    self.viewModel.models = [[self.viewModel.models reverseObjectEnumerator] allObjects];
    [self.tableView reloadData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.viewModel.models.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YLRegularFinancingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[YLRegularFinancingTableViewCell cellID]];
    cell.model = self.viewModel.models[indexPath.section];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.0001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 110;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    YLRegularFinancingDetailController *detail = [[YLRegularFinancingDetailController alloc] init];
    YLContinuationOrderModel *model = self.viewModel.models[indexPath.section];
    detail.contractCode = model.contractCode;
    detail.title = model.productName;
    [self.navigationController pushViewController:detail animated:YES];
}

- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
{
    NSString * text = @"暂无可续投订单";
    
    NSDictionary * attributes = @ { NSFontAttributeName:[UIFont boldSystemFontOfSize:14.0f ],
        NSForegroundColorAttributeName:YLLineColor};
    
    return [[ NSAttributedString  alloc ] initWithString:text attributes:attributes];
}


- (CGFloat)spaceHeightForEmptyDataSet:(UIScrollView *)scrollView
{
    return 25.0f;
}


- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView
{
    return -64;
}



@end
